package com.icoopoo.common.base.icoopoo.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.HashMap;
import java.util.IntSummaryStatistics;
import java.util.Map;
import java.util.MissingResourceException;

/**
 * @Auther:  icoopoo@163.com
 * @Date: 2018/4/25 0025
 * @Description:
 */
public class IcoopooPropertiesUtil extends PropertyPlaceholderConfigurer {

    private static Map<String,String> ctxPropertiesMap = new HashMap<>();

    public IcoopooPropertiesUtil(){
    }

    public static Map<String,String> getProperties(){
        return ctxPropertiesMap;
    }

    public static String getString(String key){
        try{
            return (String)ctxPropertiesMap.get(key);
        }catch (MissingResourceException e){
            return null;
        }
    }

    public static String getString(String key,String defaultValue){
        try{
            String value = (String)ctxPropertiesMap.get(key);
            return StringUtil.isEmpty(value) ? defaultValue : value;
        }catch (MissingResourceException e){
            return defaultValue;
        }
    }

    public static int getInt(String key){
        String value = ctxPropertiesMap.get(key);
        return value != null && !"".equals(value.trim()) ? Integer.valueOf(Integer.parseInt(value)):null;
    }

    public static int getInt(String key, int defaultValue) {
        String value = (String)ctxPropertiesMap.get(key);
        return StringUtils.isBlank(value)?defaultValue:Integer.parseInt(value);
    }

    public static long getLong(String keyName, long defaultValue) {
        String value = getString(keyName);
        if(value != null && value.length() > 0) {
            try {
                long e = Long.parseLong(value.trim());
                return e;
            } catch (Exception var6) {
                return defaultValue;
            }
        } else {
            return defaultValue;
        }
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        String value = (String)ctxPropertiesMap.get(key);
        return StringUtils.isBlank(value)?defaultValue:(new Boolean(value.trim())).booleanValue();
    }
}
